<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
	<head>
		<meta charset="utf-8">
		<title>菜品管理</title>
		<link rel="stylesheet" href="../../component/pear/css/pear.css" />
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">菜品名</label>
							<div class="layui-input-inline">
								<input type="text" name="name" placeholder="请输入要查询的菜品名" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline" >
							<label class="layui-form-label">种类</label>
							<div class="layui-input-inline">
								<select id="kind" name="kind" onclick="selectKind()">
									<option value="">--请选择--</option>
									<option value="1">---</option>
								</select>
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="food-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<!-- 数据表格 -->
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="food-table">

				</table>
			</div>
		</div>


		<script type="text/html" id="food-toolbar">

			<button class="pear-btn pear-btn-primary pear-btn-md" onclick="addFood()">
		        <i class="layui-icon layui-icon-add-1"></i>
		        添加菜品
		    </button>

		</script>



		<script type="text/html" id="food-status">
			{{#if (d.status == 0) { }}
		    <span>正常</span>
		    {{# }else if(d.status == 1){ }}
		    <span>售罄</span>
			{{# }else if(d.status == 2){ }}
			<span>其他</span>
		    {{# } }}
		</script>





		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
		<script>

			//后台我返回的是一个集合List<Map<String, Object>>

			function selectKind(){
				let $ = layui.jquery;
				$('#kind').empty();//先清空避免重复查询，此段也可省略
				$.ajax( {
					url : '/kind/list',//后台的url地址
					type : 'get',
					success : function(data) {
						console.log(data);
						$.each(data, function (i) {//data为所有的数据，i表示编号，item表示第i行的数据
							console.log(data[i]);
							$('#kind').append('<option value=" '+data.id+'">' + data.name +'</option>');
						});
					}
				});
			}



			// 编辑
			function foodEdit(id){
				layer.open({
					type: 2,
					title: '修改',
					shade: 0.1,
					area: ['750px', '800px'], // 长 宽
					content: '/food/getOneFood/'+id
				});
			}
			// 添加食品
			function addFood(){
				layer.open({
					type: 2,
					title: '添加菜品',
					shade: 0.1,
					area: ['730px', '700px'],
					content: '/food/addFoodPage'
				});
			}
			// 删除食品
			function deleteFood(id) {
				layer.confirm('确定要删除该条数据吗?', {
					icon: 3,
					title: '提示'
				}, function (index) {
					layer.close(index);
					let loading = layer.load();
					layui.jquery.ajax({
						url: '/food/deleteFood/' + id,
						type: 'delete',
						success:function(result){
							if (parseInt(result.code) === 200) {
								layer.msg(result.message, {icon: 1,time: 500},function () {
									layer.close(loading);
									parent.layui.table.reload("food-table");
								});
							} else {
								layer.msg(result.message, {icon: 2,time: 500},function () {
									layer.close(loading);
									parent.layui.table.reload("food-table");
								});
							}
						},
						error:function (result) {
							console.log("error "+result.message);
						}
					})
				})
			}


			layui.use(['table', 'form', 'jquery','common','util'], function() {
				let table = layui.table;
				let form = layui.form;
				let util = layui.util;
				let $ = layui.jquery;
				let common = layui.common;
				let MODULE_PATH = "view/system/operate/";


				let cols = [
					[{
							type: 'checkbox'
						},
						{
							title: 'ID',
							field: 'id',
							align: 'center',
							width: 120
						},
						{
							title: '名字',
							field: 'name',
							align: 'center'
						},
						{
							title: '种类',
							field: 'kname',
							align: 'center',
						},
						{
							title: '图片',
							field: 'img',
							align: 'center',
							templet: '<div> <img src="{{d.img}}"> </div>'
						},
						{
							title: '剩余库存',
							field: 'nums',
							align: 'center'
						},
						{
							title: '状态',
							field: 'status',
							align: 'center',
							templet: '#food-status'
						},
						{
							title: '修改时间',
							field: 'gmtUpdate',
							align: 'center'
						},
						{
							title: '添加时间',
							field: 'gmtCreate',
							align: 'center'
						},
						{
							title: '操作',
							align: 'center',
							width: 200,
							templet:'<div>' +
									'<button class="pear-btn pear-btn-primary pear-btn-sm" onclick="foodEdit({{d.id}})"><i class="layui-icon layui-icon-edit"></i></button> &nbsp;' +
									'<button class="pear-btn pear-btn-danger pear-btn-sm" onclick="deleteFood({{d.id}})" ><i class="layui-icon layui-icon-delete"></i></button>  &nbsp;' +
									'</div>'
						}
					]
				]


				table.render({
					elem: '#food-table',
					url: '/food/list',
					page: true,
					cols: cols,
					skin: 'line',
					method: 'post', // post请求 默认为get
					toolbar: '#food-toolbar',
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});

				table.on('tool(food-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(food-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(food-query)', function(data) {
					table.reload('food-table', {
						method:'post',
						where: data.field
					})
					return false;
				});

				form.on('switch(food-enable)', function(obj) {
					layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
				});



				window.refresh = function(param) {
					table.reload('food-table');
				}
			})
		</script>
	</body>
</html>
